package com.example.jl.bethinner.adapter;


import android.content.Context;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jl.bethinner.R;
import com.example.jl.bethinner.util.ViewHolder;

import java.util.List;

/**
 * Created by JL on 6/16/15.
 */
public class OpenStoreListAdapter extends BaseAdapter {

    private Context context;
    private List<String> names;
    private List<String> descriptions;
    private int sise;
    private Boolean isPhoneEditable;
    OpenStoreCityChooser openStoreCityChooser;

    public OpenStoreListAdapter(Context context, List<String> names, List<String> descriptions, Boolean isPhoneEditable) {
        this.context = context;
        this.names = names;
        this.descriptions = descriptions;
        this.sise = names.size();
        this.isPhoneEditable = isPhoneEditable;
        openStoreCityChooser = (OpenStoreCityChooser) context;
    }

    public void setData(List<String> names, List<String> descriptions) {
        this.names = names;
        this.descriptions = descriptions;
        this.sise = names.size();
    }


    public void setSise(int sise) {
        this.sise = sise;
    }

    public void reSise() {
        this.sise = names.size();
    }

    @Override
    public int getCount() {
        if (isPhoneEditable)
            return sise;
        else
            return sise - 1;
    }

    @Override
    public String getItem(int position) {
        return names.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public String getDescription(int position) {
        return descriptions.get(position);
    }

    public void setDescription(int position, String description) {
        descriptions.set(position, description);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null)
            convertView = LayoutInflater.from(context).inflate(R.layout.open_store_list_item_normal, parent, false);
        TextView tv_name = ViewHolder.get(convertView, R.id.name);
        final TextView get_vcode = ViewHolder.get(convertView, R.id.get_vcode);
        final EditText description = ViewHolder.get(convertView, R.id.description);
        tv_name.setText(names.get(position));

        description.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER)
                    descriptions.set(position, ((EditText) v).getText().toString());
                return false;
            }
        });
        description.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    descriptions.set(position, ((EditText) v).getText().toString());
                } else {

                    description.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {
                        }

                        @Override
                        public void afterTextChanged(Editable s) {
                            descriptions.set(position, (s.toString()));
                        }
                    });
                }
            }
        });
        switch (names.get(position)) {
            case "店铺名称":
                description.setVisibility(View.VISIBLE);
                description.setSingleLine();
                description.setInputType(InputType.TYPE_CLASS_TEXT);
                description.setText(descriptions.get(position));
                get_vcode.setVisibility(View.GONE);
                description.setHint("必填");
                break;
            case "店铺介绍":
                description.setVisibility(View.VISIBLE);
                description.setSingleLine();
                description.setInputType(InputType.TYPE_CLASS_TEXT);
                description.setText(descriptions.get(position));
                get_vcode.setVisibility(View.GONE);
                break;
            case "联系人":
                description.setVisibility(View.VISIBLE);
                description.setSingleLine();
                description.setInputType(InputType.TYPE_CLASS_TEXT);
                description.setText(descriptions.get(position));
                get_vcode.setVisibility(View.GONE);
                description.setHint("必填");
                break;

            case "所在城市":
                description.setVisibility(View.VISIBLE);
                description.setSingleLine();
                description.setInputType(InputType.TYPE_CLASS_TEXT);
                description.setText(descriptions.get(position));
                description.setEnabled(false);
                get_vcode.setVisibility(View.VISIBLE);
                get_vcode.setText("修改");
                get_vcode.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        openStoreCityChooser.openStoreCityChooser();

                    }
                });
                break;
            case "所在位置":
                description.setVisibility(View.VISIBLE);
                description.setSingleLine();
                description.setInputType(InputType.TYPE_CLASS_TEXT);
                description.setText(descriptions.get(position));
                get_vcode.setVisibility(View.GONE);
                break;
            case "电话号码":
                description.setVisibility(View.VISIBLE);
                description.setSingleLine();
                description.setInputType(InputType.TYPE_CLASS_NUMBER);
                description.setText(descriptions.get(position));
                get_vcode.setVisibility(View.GONE);
                break;
            case "手机号":
                description.setVisibility(View.VISIBLE);
                description.setSingleLine();
                description.setInputType(InputType.TYPE_CLASS_PHONE);
                description.setText(descriptions.get(position));
                get_vcode.setVisibility(View.VISIBLE);
                get_vcode.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (isPhoneEditable) {
                            getVCode(description.getText().toString());

                        } else {
                            get_vcode.setText("获取验证码");
                            isPhoneEditable = true;
                            description.requestFocus();
                            description.setEnabled(true);
                        }

                    }
                });
                if (!isPhoneEditable) {
                    get_vcode.setText("修改手机号");
                    description.setEnabled(false);
                } else {
                    get_vcode.setText("获取验证码");
                    description.requestFocus();
                    description.setEnabled(true);
                }
                break;
            case "验证码":
                description.setVisibility(View.VISIBLE);
                description.setSingleLine();
                description.setInputType(InputType.TYPE_CLASS_TEXT);
                description.setText(descriptions.get(position));
                description.setHint("必填");
                get_vcode.setVisibility(View.GONE);
                break;
        }

        return convertView;
    }

    public void getVCode(String mobile) {
        descriptions.get(0);
        Toast.makeText(context, mobile, Toast.LENGTH_SHORT).show();
    }

    public interface OpenStoreCityChooser {
        public void openStoreCityChooser();
    }
}
